package com.aaa.mrcai.controller;

import com.aaa.mrcai.annotion.AAALog;
import com.aaa.mrcai.pojo.entity.Goods;
import com.aaa.mrcai.pojo.vo.GoodsVO;
import com.aaa.mrcai.properties.OSSProperties;
import com.aaa.mrcai.query.GoodsQuery;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.GoodsService;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @Author:Li-Yan-Chun
 * @Date:2021/12/6 11:35
 */
@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private OSSProperties ossProperties;

    @RequestMapping("list")
    public AjaxResult list(GoodsQuery goodsQuery){
        return goodsService.list(goodsQuery);
    }

    @RequestMapping("listCategory")
    public AjaxResult listCategory(){
        return goodsService.listCategory();
    }

    @RequestMapping("listSpec")
    public AjaxResult listSpec(){
        return goodsService.listSpec();
    }
    @RequestMapping("listBrand")
    public AjaxResult listBrand(){
        return goodsService.lisBrand();
    }
    @RequestMapping("listGrade")
    public AjaxResult listGrade(){
        return goodsService.listGrade();
    }
    @RequestMapping("listDeposit")
    public AjaxResult listDeposit(){
        return goodsService.listDeposit();
    }

    @RequestMapping("delGoods")
    @AAALog("删除商品")
    public AjaxResult delGoods(int id){
        return goodsService.delGoods(id);
    }

    @RequestMapping("addOneGoods")
    @AAALog("添加商品")
    public AjaxResult addOneGoods(GoodsVO goodsVO){
        return goodsService.addOneGoods(goodsVO);
    }

    @RequestMapping("code")
    public AjaxResult code(){
        int random = (int) (Math.random()*100000000);
        System.out.println(random);
        return AjaxResult.success(random);
    }

    @RequestMapping("uploadGoods")
    public AjaxResult uploadGoods(GoodsVO goodsVO,MultipartFile pic) throws Exception{
        //1、判断格式、尺寸是否满足要求
        //2、审核阶段、机器鉴黄

        System.out.println(pic.getSize());
        System.out.println(pic.getContentType());
        System.out.println(pic.getInputStream());


// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());

// 填写本地文件的完整路径。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
        long time = System.currentTimeMillis();
        String imgName= UUID.randomUUID().toString()+time+pic.getContentType().replace("image/",".");

// 依次填写Bucket名称（例如examplebucket）和Object完整路径（例如exampledir/exampleobject.txt）。Object完整路径中不能包含Bucket名称。
        ossClient.putObject(ossProperties.getBucket(), imgName, pic.getInputStream());
// 关闭OSSClient。
        ossClient.shutdown();
        //真实路径
        goodsVO.setImg(ossProperties.getRealUrl()+imgName);
        System.out.println(goodsVO);
        return  goodsService.uploadGoods(goodsVO);
    }

    @RequestMapping("getOneGoods")
    public AjaxResult getOneGoods(int id){
        return goodsService.getOneGoods(id);
    }

    @RequestMapping("/getGoods/{id}")
    public AjaxResult getGoods(@PathVariable int id){
        return goodsService.getOneGoods(id);
    }

    @RequestMapping("updateOneGoods")
    @AAALog("修改商品")
    public AjaxResult updateOneGoods(GoodsVO goodsVO){
        return goodsService.updateOneGoods(goodsVO);
    }

    @RequestMapping("updateImg")
    public AjaxResult updateImg(GoodsVO goodsVO,MultipartFile pic) throws Exception{
        //1、判断格式、尺寸是否满足要求
        //2、审核阶段、机器鉴黄

        System.out.println(pic.getSize());
        System.out.println(pic.getContentType());
        System.out.println(pic.getInputStream());


// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());

// 填写本地文件的完整路径。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
        long time = System.currentTimeMillis();
        String imgName= UUID.randomUUID().toString()+time+pic.getContentType().replace("image/",".");

// 依次填写Bucket名称（例如examplebucket）和Object完整路径（例如exampledir/exampleobject.txt）。Object完整路径中不能包含Bucket名称。
        ossClient.putObject(ossProperties.getBucket(), imgName, pic.getInputStream());
// 关闭OSSClient。
        ossClient.shutdown();
        //真实路径
        goodsVO.setImg(ossProperties.getRealUrl()+imgName);
        System.out.println(goodsVO);
        return goodsService.updateImg(goodsVO);
    }
}
